<?php

namespace backend\controllers;

use common\models\Cases;
use Yii;

class CaseController extends DashboardController
{
    public function actionIndex()
    {

        return $this->render('index');
    }

    # 添加案例
    public function actionAdd()
    {

        return $this->render('add');
    }
    public function actionEdit()
    {

        return $this->render('edit');
    }

    # 保存案例
    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $item = !empty($post['id']) ? Cases::find()->where(['id' => $post['id']])->one() : new Cases();

        $saveData = [
            'name' => !empty($post['name']) ? $post['name'] : '',
            'qq' => !empty($post['qq']) ? $post['qq'] : '',
            'desc' => !empty($post['desc']) ? $post['desc'] : '',
            'tel' => !empty($post['tel']) ? $post['tel'] : '',
            'tag' => !empty($post['tag']) ? trim($post['tag']) : '',
            'wechat' => !empty($post['wechat']) ? $post['wechat'] : '',
            'time' => !empty($post['time']) ? $post['time'] : '',
            'picture' => !empty($post['picture']) ? trim($post['picture']) : '',
            'url' => !empty($post['url']) ? $post['url'] : '',
            'status' => !empty($post['status']) ? $post['status'] : 0,
        ];

        $item->attributes = $saveData;

        if ($item->validate()){
            $msg = $item->save() ? ["state" =>"success","message" =>"恭喜小主，操作成功！"] : ["state" => "error","message" => "小主大人，操作失败啦！"];
            $this->ajaxReturn($msg);
        }else{
            $this->ajaxReturn(["state" => 'error',"message" => '验证失败']);
        }
    }

}

